package com.login.login

import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.login.R
import com.login.register.RegisterFragment
import com.mineapp.base.BasePresenterFragment
import com.tool.KToast
import kotlinx.android.synthetic.main.fragment_login.*

/**
 * Created by mac on 2017/8/1.
 */
class LoginFragment : BasePresenterFragment<Constract.LoginView, Constract.LoginPresenter>(), Constract.LoginView {

    override fun bindView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater?.inflate(R.layout.fragment_login, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val textWatcher = object: TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                btn_login?.isEnabled = !(TextUtils.isEmpty(edit_phone?.text) || TextUtils.isEmpty(edit_pwd?.text))
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
        }
        edit_phone?.addTextChangedListener(textWatcher)
        edit_pwd?.addTextChangedListener(textWatcher)
        btn_login?.setOnClickListener {
            showLoading()
            mPresenter?.Login(edit_phone?.text.toString(), edit_pwd?.text.toString())
        }
        text_register.setOnClickListener {
            push(RegisterFragment::class.java)
        }
    }

    override fun bindIView(): Constract.LoginView {
        return this
    }

    override fun requestError(url: String?, code: Int, msg: String?) {
        hideLoading()
    }

    override fun updateView() {
        hideLoading()
        KToast.centerToast(activity, "哈哈")
    }

    override fun initPresenter(): Constract.LoginPresenter? {
        return LoginPresenterImpl()
    }
}